<?php
include_once '../model/Constructora.php';
include_once '../model/Obra.php';
include_once '../model/Etapa.php';
include_once '../model/Avance.php';
?>
<!DOCTYPE HTML>
<html>
    <head>
        <?php include 'content.php'; ?>
        <script type="text/javascript">
            $(document).ready(function () {

                $(".table").dataTable({
                    "oLanguage": {
                        "sLengthMenu": "Visualiza _MENU_ avances por página",
                        "sZeroRecords": "No hay avances registrados",
                        "sInfo": "Mostrando _START_ de _END_ de _TOTAL_ avances",
                        "sInfoEmpty": "Mostrando 0 de 0 de 0 avance",
                        "sInfoFiltered": "(filtrado de _MAX_ avances)",
                        "sSearch": "Buscar",
                        "oPaginate": {
                            "sFirst": " Primera ",
                            "sNext": " Siguiente ",
                            "sPrevious": " Anterior ",
                            "sLast": " Ultima "
                        }
                    },
                    "bLengthChange": false,
                    "iDisplayLength": 10
                }); 
                
                $("#dialog-form").dialog({
                    autoOpen: false,
                    height: 500,
                    width: 730,
                    modal: true,
                    buttons: {                        
                        "Cerrar": function () {
                            $(this).dialog("close");
                        }
                    }
                });
               
            }); 
                   
            
            function viewPdf(id){
                $("#idAvance").val(id);
                $.ajax({
                    data:  $("#formPdf").serialize(),
                    url:   '<?php echo $_SERVER['HELPER'] ?>?action=avance&method=reporteAjax&result=ajax',
                    type:  'post',
                    beforeSend: function () {
                    },
                    success:  function (data) {               
                        
                        var info = JSON.parse(data);
                                                                                                               
                        if (info.estatus == AJAX_SUCCESSFUL) {
                            $("#pdf").attr("src", info.path);
                            $("#dialog-form").dialog("open");

                        }else{
                            alert(info.msj);
                        }                                 
                    }                                    
                });                
            }
            
            function comentarios(id){
                $("#id").val(id);
                $("#form").attr('action','<?php echo $_SERVER['HELPER'] ?>?action=comentarios&method=listar');
                $("#form").submit();                 
            }                        
           
        </script>
    </head>
    <body>      
        <?php include_once 'header.php'; ?> 
        <div id="main">
            <div class="column-left">
                <?php include_once 'lateral.php'; ?> 
            </div>
            <div class="column-center" style="text-align: right;">
                <div style="text-align: left;">
                    <h2>Avances Registrados</h2>
                </div>                
                <table id="data-u" border="0" class="table">
                    <thead>
                        <tr>
                            <th>FECHA</th>
                            <th>INSPECTOR</th>
                            <th></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if (isset($_SESSION['avancesDelMes'])) {
                            $i = 0;
                            $avance = new Avance();
                            $listAvances = unserialize($_SESSION['avancesDelMes']);

                            for ($i = 0; $i < count($listAvances); $i++) {
                                $avance = $listAvances[$i];
                                ?>                                
                                <tr id="<?php echo $avance->getId(); ?>">
                                    <td><?php echo date("d/m/Y", strtotime($avance->getFechaAvance())) ?></td>
                                    <td><?php echo $avance->getInspector()->getNombre() . ' ' . $avance->getInspector()->getApellido() ?></td>                                
                                    <td>
                                        <img src="../images/comments_accept.png" alt="" class="pointer" width="16" height="16"
                                             onclick="comentarios(<?php echo $avance->getId(); ?>);"
                                             title="Comentarios"/>
                                    </td>
                                    <td>
                                        <img src="../images/pdf.png" alt="" class="pointer" width="16" height="16"
                                             onclick="viewPdf(<?php echo $avance->getId(); ?>);"
                                             title="Ver Planilla"/>
                                    </td>
                                </tr>
                            <?php }
                        } ?>
                    </tbody>
                </table>
                <form id="form" name="form" method="post" action="">
                    <input type="hidden" id="id" name="id"/>
                </form>
                <br/><br/>
            </div>
            <div class="column-right">
                <?php include 'right.php'; ?> 
            </div>
        </div>
        <?php include_once 'footer.php'; ?>    
        <div id="dialog-form" title="Ficha de Avance">
            <form id="formPdf" name="formPdf" method="post" action="#">
                <iframe id="pdf" src="" style="height: 380px; width: 700px"> </iframe>    
                <input type="hidden" name="idAvance" id="idAvance" />
            </form>
        </div>
    </body>    
</html>